package com.molichuxing.services.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.services.business.convertor.Convert;
import com.molichuxing.services.business.dto.request.create.EntrepotStorageBizCreateDto;
import com.molichuxing.services.business.dto.request.modify.EntrepotStorageBizModifyDto;
import com.molichuxing.services.business.dto.response.EntrepotStorageBizDto;
import com.molichuxing.services.business.service.EntrepotStorageBizService;
import com.molichuxing.services.infrastructure.dto.request.create.EntrepotStorageCreateDto;
import com.molichuxing.services.infrastructure.dto.request.create.StockCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EntrepotStorageModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.StockModifyDto;
import com.molichuxing.services.infrastructure.dto.response.EntrepotStorageDto;
import com.molichuxing.services.infrastructure.dto.response.StockDto;
import com.molichuxing.services.infrastructure.service.EntrepotStorageService;
import com.molichuxing.services.infrastructure.service.StockService;
import com.molichuxing.services.property.StockTypeEnum;

/**
 * 
 * @author liyg
 * 中转库组合服务实现类
 * 2019-08-20
 *
 */
@Service("entrepotStorageBizService")
public class EntrepotStorageBizServiceImpl implements EntrepotStorageBizService{
	
	@Autowired
	private EntrepotStorageService entrepotStorageService;
	
	@Autowired
	private StockService stockService;

	/**
	 *获取中转库列表
	 * @param map 参数
	 *key:shortName  String 中转库简称 非必填
	 *key:name       String 负责人姓名 非必填
	 *key:regionId   Integer 城市id 非必填
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<EntrepotStorageBizDto> getEntrepotStorageList(Map<String, Object> map) {
		
		Paged<EntrepotStorageDto> paged = entrepotStorageService.getEntrepotStorageList(map);
		if(null == paged) {
			return null;
		}else if(paged.isEmpty()) {
			return new Paged<>(null,paged.getTotal(),paged.getPageSize());
		}
		
		List<EntrepotStorageBizDto> entrepotStorageBizDtoList = Convert.toEntrepotStorageBizDto(paged.getList());
		
		List<Integer> ids=new ArrayList<>();
		for (EntrepotStorageBizDto bizDto : entrepotStorageBizDtoList) {
			ids.add(bizDto.getId());
		}
		
		Map<Integer, StockDto> stockMap = stockService.getStocks(ids);
		if(null != stockMap) {
			for (EntrepotStorageBizDto bizDto : entrepotStorageBizDtoList) {
				StockDto stockDto = stockMap.get(bizDto.getId());
				bizDto.setType(stockDto==null ? null : stockDto.getType());
				bizDto.setLongitude(stockDto==null ? null : stockDto.getLongitude());
				bizDto.setDimension(stockDto==null ? null : stockDto.getDimension());
			}
		}
		
		Long total=paged.getTotal();
		Integer pageSize=paged.getPageSize();
		Paged<EntrepotStorageBizDto> page=new Paged<>(entrepotStorageBizDtoList,total,pageSize);
		
		return page;
	}

	/**
	 * 获取中转库详情
	 * 
	 * @param id 中转库id
	 * @return
	 */
	@Override
	public EntrepotStorageBizDto getEntrepotStorage(Integer id) {
		
		EntrepotStorageDto entrepotStorage = entrepotStorageService.getEntrepotStorage(id);
		EntrepotStorageBizDto entrepotStorageBizDto = Convert.toEntrepotStorageBizDto(entrepotStorage);
		if(null == entrepotStorageBizDto) {
			return null;
		}
		
		StockDto stockDto = stockService.getStock(id);
		if(null == stockDto) {
			return null;
		}
		entrepotStorageBizDto.setType(stockDto.getType());
		entrepotStorageBizDto.setLongitude(stockDto.getLongitude());
		entrepotStorageBizDto.setDimension(stockDto.getDimension());
		
		return entrepotStorageBizDto;
	}

	/**
	 * 保存中转库信息
	 * 
	 * @param createDto
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	@Override
	public Integer create(EntrepotStorageBizCreateDto createDto) {
		
		StockCreateDto stockCreateDto=new StockCreateDto();
		BeanUtils.copyProperties(createDto, stockCreateDto);
		stockCreateDto.setType(StockTypeEnum.ENTREPOT_STORAGE.getValue());
		Integer id = stockService.create(stockCreateDto);
		if(null == id) {
			throw new RuntimeException();
		}
		
		EntrepotStorageCreateDto entrepotStorageCreateDto=new EntrepotStorageCreateDto();
		BeanUtils.copyProperties(createDto, entrepotStorageCreateDto);
		entrepotStorageCreateDto.setId(id);
		entrepotStorageCreateDto.setApplyTime(DateUtil.convertDateToLDT(new Date()));
		return entrepotStorageService.create(entrepotStorageCreateDto);
	}

	/**
	 * 修改中转库信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	@Override
	public Boolean modify(EntrepotStorageBizModifyDto modifyDto) {
		
		StockModifyDto stockModifyDto=new StockModifyDto();
		BeanUtils.copyProperties(modifyDto, stockModifyDto);
		Boolean flag = stockService.modify(stockModifyDto);
		if(!flag) {
			throw new RuntimeException();
		}
		
		EntrepotStorageModifyDto entrepotStorageModifyDto=new EntrepotStorageModifyDto();
		BeanUtils.copyProperties(modifyDto, entrepotStorageModifyDto);
		flag = entrepotStorageService.modify(entrepotStorageModifyDto);
		if(!flag) {
			throw new RuntimeException();
		}
		
		return flag;
	}

}
